/*
 * FrameNota.java
 *
 * Created on 2 de Julho de 2007, 11:57
 */
package br.com.grillo.jturbonote;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.InetAddress;
import static br.com.grillo.jturbonote.Configuracao.*;
import static br.com.grillo.jturbonote.Main.*;

/**
 *
 * @author  grillo
 */
public class FrameNota extends javax.swing.JDialog {

    private Nota nota;
    private int clique;

    /** Creates new form FrameNota */
    public FrameNota() {
        initComponents();
        configuraNota();
        popMenu.add(miStayOnTop);
        popMenu.add(miEnviar);
        popMenu.add(miDetalhes);
        popMenu.add(miSair);
        txtMensagem.requestFocus();
        txtTitulo.setText(getDataFormatada(System.currentTimeMillis()));
    }

    private void configuraNota() {
        Color cor;
        if (getValor("cor-fundo") != null) {
            cor = new Color(getValorInt("cor-fundo"));
            txtMensagem.setBackground(cor);
        }

        if (getValor("cor-titulo") != null) {
            cor = new Color(getValorInt("cor-titulo"));
            txtTitulo.setBackground(cor);
        }

        if (getValor("cor-fonte") != null) {
            cor = new Color(getValorInt("cor-fonte"));
            txtTitulo.setForeground(cor);
            txtMensagem.setForeground(cor);
        }

        if ("true".equals(getValor("stay-on-top", "false"))) {
            miStayOnTop.setSelected(true);
            setAlwaysOnTop(true);
        }

        if (getValor("w-nota") != null && getValor("h-nota") != null) {
            setSize(getValorInt("w-nota"), getValorInt("h-nota"));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popMenu = new javax.swing.JPopupMenu();
        miSair = new javax.swing.JMenuItem();
        miStayOnTop = new javax.swing.JCheckBoxMenuItem();
        miEnviar = new javax.swing.JMenuItem();
        miDetalhes = new javax.swing.JMenuItem();
        jspMensagem = new javax.swing.JScrollPane();
        txtMensagem = new javax.swing.JTextArea();
        txtTitulo = new javax.swing.JTextField();

        miSair.setMnemonic('S');
        miSair.setText("Sair");
        miSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miSairActionPerformed(evt);
            }
        });
        miSair.getAccessibleContext().setAccessibleParent(popMenu);

        miStayOnTop.setText("Stay on Top");
        miStayOnTop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miStayOnTopActionPerformed(evt);
            }
        });

        miEnviar.setText("Enviar");
        miEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miEnviarActionPerformed(evt);
            }
        });

        miDetalhes.setMnemonic('D');
        miDetalhes.setText("Detalhes");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);
        setUndecorated(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        txtMensagem.setColumns(5);
        txtMensagem.setLineWrap(true);
        txtMensagem.setRows(1);
        txtMensagem.setWrapStyleWord(true);
        txtMensagem.setComponentPopupMenu(popMenu);
        txtMensagem.setDragEnabled(true);
        txtMensagem.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtMensagemKeyPressed(evt);
            }
        });
        jspMensagem.setViewportView(txtMensagem);

        txtTitulo.setEditable(false);
        txtTitulo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtTituloMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                txtTituloMousePressed(evt);
            }
        });
        txtTitulo.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                txtTituloMouseDragged(evt);
            }
        });
        txtTitulo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtTituloFocusLost(evt);
            }
        });
        txtTitulo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTituloKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(txtTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
            .addComponent(jspMensagem, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(txtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspMensagem, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtTituloKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTituloKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            txtMensagem.requestFocus();
        } else {
            formKeyPressed(evt);
        }
}//GEN-LAST:event_txtTituloKeyPressed

private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
    String modifiers = KeyEvent.getKeyModifiersText(evt.getModifiers());

    if (evt.getKeyCode() == KeyEvent.VK_ENTER && modifiers.equalsIgnoreCase("CTRL")) {
        enviarTexto();
    } else if (evt.getKeyCode() == KeyEvent.VK_T && modifiers.equalsIgnoreCase("CTRL")) {
        miStayOnTop.setSelected(!isAlwaysOnTop());
        setAlwaysOnTop(!isAlwaysOnTop());
    } else if (evt.getKeyCode() == KeyEvent.VK_F4 && modifiers.equalsIgnoreCase("CTRL")) {
        dispose();
    } else if (evt.getKeyCode() == KeyEvent.VK_LEFT && modifiers.equalsIgnoreCase("CTRL")) {
        setSize(getWidth() - 10, getHeight());
    } else if (evt.getKeyCode() == KeyEvent.VK_RIGHT && modifiers.equalsIgnoreCase("CTRL")) {
        setSize(getWidth() + 10, getHeight());
    } else if (evt.getKeyCode() == KeyEvent.VK_UP && modifiers.equalsIgnoreCase("CTRL")) {
        setSize(getWidth(), getHeight() - 10);
    } else if (evt.getKeyCode() == KeyEvent.VK_DOWN && modifiers.equalsIgnoreCase("CTRL")) {
        setSize(getWidth(), getHeight() + 10);
    }
}//GEN-LAST:event_formKeyPressed

private void txtTituloMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTituloMouseDragged
    setLocation(evt.getXOnScreen(), evt.getYOnScreen());
}//GEN-LAST:event_txtTituloMouseDragged

private void txtTituloMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTituloMousePressed
    }//GEN-LAST:event_txtTituloMousePressed

private void txtTituloFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTituloFocusLost
    clique = 0;
    txtTitulo.setEditable(false);
}//GEN-LAST:event_txtTituloFocusLost

private void txtTituloMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTituloMouseClicked
    if ((clique++) > 0) {
        txtTitulo.setEditable(true);
    }
}//GEN-LAST:event_txtTituloMouseClicked

private void miEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miEnviarActionPerformed
    enviarTexto();
}//GEN-LAST:event_miEnviarActionPerformed

private void miStayOnTopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miStayOnTopActionPerformed
    nota.setProperty("StayOnTop", miStayOnTop.isSelected() ? "Yes" : "No");
    setAlwaysOnTop(miStayOnTop.isSelected());
}//GEN-LAST:event_miStayOnTopActionPerformed

private void miSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miSairActionPerformed
    sair();
}//GEN-LAST:event_miSairActionPerformed

    public void sair() {
        dispose();
        MenuSistema.getInstance().removeNota(this);
    }

private void txtMensagemKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMensagemKeyPressed
    formKeyPressed(evt);
}//GEN-LAST:event_txtMensagemKeyPressed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing

}//GEN-LAST:event_formWindowClosing

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    sair();
}//GEN-LAST:event_formWindowClosed

    private void enviarTexto() {
        FrameEnviar enviar = new FrameEnviar();

        InetAddress para = null;
        if (nota == null) {
            nota = new Nota();
            enviar.setNota(this, null);
        } else {
            para = nota.getSource();
        }

        nota.setMessage(txtMensagem.getText());
        nota.setBounds(getBounds());
        if (txtTitulo.getText().equals(getDataFormatada(System.currentTimeMillis()))) {
            nota.setProperty("SZTITLE", "");
        } else {
            nota.setProperty("SZTITLE", txtTitulo.getText());
        }
        enviar.setNota(this, para);
        enviar.setVisible(true);
    }

    public void setNota(Nota nota) {
        this.nota = nota;
        if (nota == null) {
            setVisible(false);
        } else {
            setBounds(nota.getBounds());
            setTitle(nota.toString());
            txtTitulo.setText(nota.toString());
            txtMensagem.setText(nota.getMessage());
            txtMensagem.setCaretPosition(txtMensagem.getText().length());
            if (nota.getAttrs("StayOnTop").equalsIgnoreCase("Yes")) {
                setAlwaysOnTop(true);
            }
            requestFocus();
            txtMensagem.requestFocus();
        }
    }

    public Nota getNota() {
        return nota;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jspMensagem;
    private javax.swing.JMenuItem miDetalhes;
    private javax.swing.JMenuItem miEnviar;
    private javax.swing.JMenuItem miSair;
    private javax.swing.JCheckBoxMenuItem miStayOnTop;
    private javax.swing.JPopupMenu popMenu;
    private javax.swing.JTextArea txtMensagem;
    private javax.swing.JTextField txtTitulo;
    // End of variables declaration//GEN-END:variables
}
